package edu.ynu.se.xiecheng.achitectureclass.service;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
public class InitializeService {
    private final GreenhouseService greenhouseService;
    private final DeviceService deviceService;
    private final UserService userService;

    public InitializeService(GreenhouseService greenhouseService, DeviceService deviceService, UserService userService) {
        this.greenhouseService = greenhouseService;
        this.deviceService = deviceService;
        this.userService = userService;
    }

    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(2)
    public void init() throws IOException {
        greenhouseService.addDefaultGreenhouse();
        deviceService.addDefaultDevice();
        deviceService.getDevicesData();
        deviceService.getNewCameraPhoto();
        userService.addDefaultUser();
    }
}
